package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.mapper.MailLogMapper;
import com.xxxx.server.po.EmployeeVo;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.IEmployeeService;
import com.xxxx.server.utils.JavaBeanUtils;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Wrapper;
import java.util.*;
import java.util.concurrent.TimeoutException;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhoubin
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

	@Resource
	private EmployeeMapper employeeMapper;
	@Autowired
	private RabbitTemplate rabbitTemplate;
	@Resource
	private MailLogMapper mailLogMapper;



	@Override
	public int updateSalary(Integer eid, Integer sid) {
		Employee employee=new Employee();
		employee.setId(eid);
		employee.setSalaryId(sid);
		int i = baseMapper.updateById(employee);
		return i;
	}

	@Override
	public Map<String,Object> selectAllList(EmployeeVo vo) {
		//开启分页
		PageHelper.startPage(vo.getCurrentPage(),vo.getSize());
		List<Employee> list=employeeMapper.selectAllList(vo);
		PageInfo<Employee> pageInfo = new PageInfo(list);
		Map<String, Object> map=new HashMap<>();
		map.put("code",200);
		map.put("message","");
		map.put("total",pageInfo.getTotal());
		map.put("data", list);

		return map;
	}

	@Override
	public void deleteUser(Integer id) {
		employeeMapper.deleteUser(id);
		System.out.println(id);
	}

	@Override
	public List<Position> selectPosition() {
		return employeeMapper.selectPosition();
	}


	@Override
	public List<Nation> selectNation() {
		return employeeMapper.selectNation();
	}

	@Override
	public List<PoliticsStatus> selectPoliticsStatus() {

		return employeeMapper.selectPoliticsStatus();
	}

    @Override
    public List<Department> selectDepartment() {

		return employeeMapper.selectDepartment();
    }


	@Override
    public List<Employee> selectWorkId() {

		return employeeMapper.selectWorkId();
    }

	@Override
	public List<Joblevel> selectJobLevels() {

		return employeeMapper.selectJobLevels();
	}

	/**
	 * 更新员工信息
	 * @param employee
	 * @return
	 */
    @Override
    public RespBean updateEmployee(Employee employee) {
        return JavaBeanUtils.check(employeeMapper.updateEmployee(employee));
    }

	/**
	 * 添加员工
	 * @param employee
	 * @return
	 */
	@Override
	public RespBean insertEmployee(Employee employee){
		//添加员工
		RespBean respBean = JavaBeanUtils.check(employeeMapper.insertEmployee(employee));
		//把刚刚添加的员工查询出来
		Employee newEmp = employeeMapper.selectEmployeeById(employee.getId());
		//发送邮件
		if (respBean.getCode() == 200) rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME, MailConstants.MAIL_ROUTING_KEY_NAME,newEmp,new CorrelationData(UUID.randomUUID().toString()));

		return respBean;
	}

    @Override
    public List<Employee> selectAllEmployee() {
		return employeeMapper.selectAllEmployee();

    }


}
